﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ZKLibrarys.zkfp
{
    /// <summary>
    /// 中控指纹采集仪类
    /// </summary>
    public class ZKFingerAPI
    {
        #region 数据定义
        public AxZKFPEngXControl.AxZKFPEngX ZKFP { get; set; }
        private bool ZKFP_INIT = false;
        private int DB_HANDLE ;
        public delegate void CaptionEventHandle(object obj);
        public delegate void ImageEventHandle(Bitmap bitmap);

        public event ImageEventHandle OnImage;
        #endregion

        #region 构造释放
        public ZKFingerAPI(int ID)
        {
            ZKFP = new AxZKFPEngXControl.AxZKFPEngX();
            ZKFP.SensorIndex = ID;
            if (ZKFP.InitEngine() != 0)
            {
                ZKFP.EndEngine();
                throw new Exception("中控指纹仪连接失败!");
            }
            ZKFP.FakeFunOn = 1;
            ZKFP.FPEngineVersion = "10";
            DB_HANDLE = ZKFP.CreateFPCacheDB();

            ZKFP_INIT = true;
        }
        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="ID">采集仪ID</param>
        public ZKFingerAPI(int ID, bool FakeFunOn,string FPEngineVersion)
        {
            ZKFP = new AxZKFPEngXControl.AxZKFPEngX();
            ZKFP.SensorIndex = ID;
            if (ZKFP.InitEngine() != 0)
            {
                ZKFP.EndEngine();
                throw new Exception("中控指纹仪连接失败!");
            }
            ZKFP.FakeFunOn = FakeFunOn ? 1 : 0;
            ZKFP.FPEngineVersion = FPEngineVersion;
            ZKFP.OnCapture += ZKFP_OnCapture;
            ZKFP.OnEnroll += ZKFP_OnEnroll;
            ZKFP.OnImageReceived += ZKFP_OnImageReceived;
            ZKFP.OnFeatureInfo += ZKFP_OnFeatureInfo;

            ZKFP_INIT = true;
        }

        

        /// <summary>
        /// 获取到图像事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZKFP_OnImageReceived(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnImageReceivedEvent e)
        {
            object obj = null;
            if (e.aImageValid)
            {
                if(ZKFP.GetFingerImage(ref obj))
                {
                    OnImage?.Invoke(new Bitmap(new MemoryStream(obj as byte[])));
                }
                else
                {
                    OnImage?.Invoke(new Bitmap(1, 1));
                }
            }
            
        }

        private void ZKFP_OnFeatureInfo(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnFeatureInfoEvent e)
        {

        }

        private void ZKFP_OnEnroll(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnEnrollEvent e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 取到用于比对的指纹验证模板
        /// </summary>
        private void ZKFP_OnCapture(object sender, AxZKFPEngXControl.IZKFPEngXEvents_OnCaptureEvent e)
        {
            //指纹质量分数
            int score = 8;
            //指纹比对次数
            int processedNum = 1;
            //确定取么用于比对的指纹验证模板
            if (e.actionResult)
            {
                int ID = ZKFP.IdentificationInFPCacheDB(DB_HANDLE, e.aTemplate, ref score, ref processedNum);
                if (ID == -1)
                {

                }
                else
                {

                }
            }
        }

        /// <summary>
        /// 获取或设置是否开启防假手指功能
        /// </summary>
        public bool FakeFunOn
        {
            get
            {
                if (ZKFP.FakeFunOn == 0) { return false; }
                if (ZKFP.FakeFunOn == 1) { return true; }
                throw new Exception("FakeFunOn读取出错");
            }
            set
            {
                if (value)
                {
                    ZKFP.FakeFunOn = 1;
                }
                else
                {
                    ZKFP.FakeFunOn = 0;
                }
            }
        }

        /// <summary>
        /// 获取或设置指纹版本号
        /// </summary>
        public string FPEngineVersion
        {
            get
            {
                return ZKFP.FPEngineVersion;
            }
            set
            {
                ZKFP.FPEngineVersion = value;
            }
        }
        #endregion

        /// <summary>
        /// 设置指纹高速缓存
        /// </summary>
        /// <param name="keyValues">指纹数据</param>
        /// <returns></returns>
        public List<bool> SetCacheData(Dictionary<int,byte[]> keyValues)
        {
            List<bool> lb = new List<bool>();
            foreach (var item in keyValues)
            {
                lb.Add(ZKFP.AddRegTemplateToFPCacheDB(DB_HANDLE, item.Key, item.Value) == 0);
            }
            return lb;
        }

        /// <summary>
        /// 获取指纹当前指纹
        /// </summary>
        public void GetFinger()
        {

        }

    }
}
